interface Player {
  bankerCount: number,
  lftId: number,
  nickname: string,
  owner: boolean,
  point: number,
  seatIndex: number,
  suitDescribe: Array<{name: string, count: number}>,
  suitMap: any,
  suitPatterns: any,
  uid: string,
  winner: boolean
}

declare var window: any

class UserLabel extends eui.Component {
  private userGroup: eui.Group
  private labelBg: eui.Image
  private userid: eui.Label
  private userContent: eui.Group
  private userScore: eui.Label
  private username: eui.Label
  public player: Player
  constructor(player) {
    super()
    this.once(eui.UIEvent.COMPLETE, this.onComplete, this)
    this.player = player
    this.skinName = `<e:Skin class="MajiangSkin" width="220" height="320" xmlns:e="http://ns.egret.com/eui" xmlns:w="http://ns.egret.com/wing">
      <e:Image verticalCenter="0" horizontalCenter="0" id="labelBg" width="220" height="307"></e:Image>
      <e:Group verticalCenter="0" horizontalCenter="0" id="userGroup" width="220" height="307">
        <e:Label id="username" text="" textColor="0x5c2d0d" size="24" right="8" y="30" horizontalCenter="20" percentWidth="60" textAlign="center" height="30"></e:Label>
        <e:Label id="userid" text="" x="0" textColor="0x5c2d0d" y="70" size="24" height="43" percentWidth="100" textAlign="center"></e:Label>
        <e:Group id="userContent" x="0" y="87" width="180" height="168">
        </e:Group>
        <e:Label size="60" id="userScore" text="" x="0" bottom="10" percentWidth="100" textAlign="center" height="104"></e:Label>
      </e:Group>
    </e:Skin>`
  }

  scoreNumber(point): Array<string> {
    var pointSigned = point > 0 ? '+' + point : point
    var numbers = (pointSigned + '').split('')
    return numbers.map(num => {
      if (point >= 0) {
        return `${num}_png`
      }
      if (num === '-') {
        return '-_png'
      }
      return `1${num}_png`
    })
  }

  private onComplete(e: eui.UIEvent) {
    const player = this.player
    const bgSrc = player.point >= 0 ? 'light-di_png' : 'default-di_png'
    this.labelBg.source = bgSrc

    var vLayout:eui.VerticalLayout = new eui.VerticalLayout()
    vLayout.gap = 10
    vLayout.paddingTop = 5
    vLayout.horizontalAlign = egret.HorizontalAlign.CENTER
    this.userContent.layout = vLayout //设置问垂直布局

    this.player.suitDescribe.forEach(element => {
      const label = new eui.Label(`${element.name}: ${element.count}`)
      label.size = 23
      this.userContent.addChild(label)
    })

    this.userid.text = this.player.lftId + ''
    this.username.text = this.player.nickname.substring(0, 6)

    if (this.player.winner) {
      const winner = new eui.Image('big-winner_png')
      winner.right = -3
      winner.bottom = -5
      this.userGroup.addChild(winner)
      // this.username.textColor = 0xDC143C
    }
    if (this.player.owner) {
      const owner = new eui.Image('owner_niuniu_png')
      owner.right = 0
      owner.top = 0
      this.userGroup.addChild(owner)
    }

    this.userScore.stroke = 1
    if (this.player.point >= 0) {
      this.userScore.textColor = 0xdf3b0f
      this.userScore.strokeColor = 0xdf3b0f
      this.userScore.text = '+' + this.player.point
    } else {
      this.userScore.textColor = 0x12c52f
      this.userScore.strokeColor = 0x12c52f
      this.userScore.text = '' + this.player.point
    }

    if (this.$hasAddToStage) {
      window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
    } else {
      this.once(eui.UIEvent.ADDED_TO_STAGE, function () {
        setTimeout(() => {
          window.eventDispatcher.dispatchEvent(new egret.Event('ok'))
        }, window.labelRenderTime)
      }, this)
    }
  }
}

export default UserLabel